package com.jackie.tongzhuang.Controls;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.jackie.tongzhuang.R;

public class OptionItem extends LinearLayout implements View.OnClickListener{

    private TextView tv_text;

    private String text = "";

    private int value = 0;

    private int sel_val = 0;



    public OptionItem(Context context) {
        super(context);
    }

    public OptionItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.control_option, this);
        tv_text = view.findViewById(R.id.tv_text);
        tv_text.setBackgroundResource(R.drawable.checkbox_bg);

        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.Option);
        value = t.getInt(R.styleable.Option_value,0);
        text = t.getString(R.styleable.Option_text);
        tv_text.setOnClickListener(this);

        if(text != null){
            tv_text.setText(text);
        }

        if(value != 0){
            sel_val = value;
        }
    }

    public void setValue(int val){
        sel_val = val;
    }

    public int getValue(){
        return sel_val;
    }

    public void setText(String str){
        text = str;
        tv_text.setText(str);
    }

    public String getText(){
        return text;
    }

    public void setColor(int color, int drawable){
        tv_text.setTextColor(color);
        tv_text.setBackgroundResource(drawable);
    }


    private void initOptionType(OptionItem qo,String color_type){
        int draw = 0;
        int color = 0;
        switch (color_type){
            case "选择":
                color = getResources().getColor(R.color.selectColor);
                draw = R.drawable.rect_line_select;
                break;
            default:
                color = getResources().getColor(R.color.textColor);
                draw = R.drawable.rect_line_default;
                break;
        }
        qo.setColor(color,draw);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_text:
                sel_val = this.getValue();
                Log.v("msgg","text selected"+sel_val+"=="+this.getValue());
                setSelected(sel_val);
                break;
        }
    }

    /**
     * 设置初始化时默认选中项，
     * @param sel_id  默认选中项序号，从1开始
     */
    public void setSelected(int sel_id){
        GridRadioGroup group = (GridRadioGroup) this.getParent();
        int childCount = group.getChildCount();
        for (int i = 0; i < childCount; i++) {
            OptionItem child = (OptionItem) group.getChildAt(i);
            initOptionType(child,"");
        }
        OptionItem sel_child = (OptionItem) group.getChildAt(sel_id - 1);
        initOptionType(sel_child,"选择");
        sel_val = sel_child.getValue();
        triggerParentEvent();
    }



    public interface OnSelectedListener {
        void onSelected(int sel_val);
    }

    private OnSelectedListener listener;

    public void setOnSelectedListener(OnSelectedListener _listener) {
        this.listener = _listener;
    }

    // 触发事件的方法
    public void triggerParentEvent() {
        if (listener != null) {
            listener.onSelected(sel_val);
        }
    }
}
